package net.xujialiang.XTestRPA.Controller.RPAManager;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Entity.RPA.XRPATask;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.RPA.XRPATaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/rpatask")
@RestController
public class RPATaskMgr {
    @Autowired
    XRPATaskService taskService;

    @RequestMapping(value = "/createTask", method = RequestMethod.POST)
    @ResponseBody
    public ResultData createTask(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        try {
            taskService.createTask(reqBody, createAccount, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/deleteTask", method = RequestMethod.POST)
    @ResponseBody
    public ResultData delete(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer id = (Integer) reqBody.get("id");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            taskService.delete(id, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/fetchTasks", method = RequestMethod.POST)
    @ResponseBody
    public ResultData<Map<String, Object>> fetch(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer page = (Integer) reqBody.get("page");
        Integer pageSize = (Integer) reqBody.get("pageSize");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        String groupId = (String) reqBody.getOrDefault("groupId", null);
        String taskName = (String) reqBody.getOrDefault("taskName", null);
        List<String> tagIds = (List<String>) reqBody.getOrDefault("tagIds", new ArrayList<>());
        try {
            Map<String, Object> ret = taskService.fetchByPage(groupId, taskName, tagIds, ttc, page, pageSize);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/fetchTaskById", method = RequestMethod.POST)
    @ResponseBody
    public ResultData<XRPATask> fetchTaskById(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer id = (Integer) reqBody.get("id");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            XRPATask ret = taskService.fetchById(id, ttc);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public ResultData order(@RequestBody Map<Integer, Integer> reqBody, HttpServletRequest request, Authentication auth) {
        try {
            SecurityUser sUser = (SecurityUser) auth.getPrincipal();
            String ttc = sUser.getTTC();
            taskService.updateTaskOrder(reqBody, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }

    }

    @RequestMapping(value = "/updateTask", method = RequestMethod.POST)
    @ResponseBody
    public ResultData update(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            taskService.update(reqBody, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/updateTaskFlowData", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateTaskFlow(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        String taskData = (String) reqBody.getOrDefault("taskData", "");
        Integer costPoint = (Integer) reqBody.get("costPoint");
        Integer id = (Integer) reqBody.get("id");
        try {
            taskService.updateTaskData(id, taskData, costPoint, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/executeTask", method = RequestMethod.POST)
    @ResponseBody
    public ResultData executeTask(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        Integer id = (Integer) reqBody.get("id");
        Integer costPoint = (Integer) reqBody.get("costPoint");
        String user = sUser.getUsername();
        try {
            taskService.executeTask(id, costPoint, ttc, user);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }
}
